﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.ClientDisplayMechanics {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Interfaces.*;
    import flash.display.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;
    import Classes.*;
    import com.adobe.utils.*;

    public class MultiAssetRenderer implements IClientGameMechanic {

        public static const DEFAULT_IMAGE_NAME:String = "static";

        protected var m_owner:MechanicMapResource;
        protected var m_type:String;
        protected var m_dataSourceType:String;
        protected var m_ownerDisplayObj:Sprite;
        protected var m_displayLocations:Array;
        protected var m_displayContentMap:Dictionary;
        private var m_isDirty:Boolean = true;

        public function MultiAssetRenderer(){
            this.m_displayLocations = new Array();
            this.m_displayContentMap = new Dictionary();
        }
        public function updateDisplayObject(_arg1:Number):void{
            var _local2:Object;
            var _local3:DisplayObject;
            var _local4:IDynamicDisplayObject;
            if (this.m_isDirty){
                this.m_isDirty = false;
                this.refreshDisplayData();
            };
            for each (_local2 in this.m_displayLocations) {
                if (!this.m_displayContentMap[_local2.id]){
                } else {
                    _local3 = this.m_displayContentMap[_local2.id];
                    if (!this.m_ownerDisplayObj.contains(_local3)){
                        this.m_ownerDisplayObj.addChild(_local3);
                    };
                    if ((_local3 is IDynamicDisplayObject)){
                        _local4 = (_local3 as IDynamicDisplayObject);
                        _local4.onUpdate(_arg1);
                    };
                };
            };
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            var _local3:XML;
            var _local4:Object;
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_type = _arg2.type;
            this.m_dataSourceType = _arg2.params["dataSourceType"];
            this.m_ownerDisplayObj = (this.m_owner.getDisplayObject() as Sprite);
            if (!_arg2.rawXMLConfig.hasOwnProperty("displayLocations")){
                return;
            };
            for each (_local3 in _arg2.rawXMLConfig.displayLocations.location) {
                _local4 = new Object();
                _local4.id = int(_local3.@id);
                _local4.x = Number(_local3.@mapX);
                _local4.y = Number(_local3.@mapY);
                this.m_displayLocations.push(_local4);
            };
            this.m_owner.addEventListener(GenericObjectEvent.MECHANIC_DATA_CHANGED, this.onMechanicDataChanged, false, 0, true);
            this.m_owner.getItem().addEventListener(LoaderEvent.LOADED, this.onOwnerItemImageLoaded, false, 1, true);
            this.m_owner.addEventListener(GameObjectEvent.STATE_CHANGE, this.onStateChanged, false, 0, true);
        }
        protected function onMechanicDataChanged(_arg1:GenericObjectEvent):void{
            if (_arg1.obj == this.m_dataSourceType){
                this.m_isDirty = true;
            };
        }
        protected function onStateChanged(_arg1:GameObjectEvent):void{
            this.m_ownerDisplayObj = (this.m_owner.getDisplayObject() as Sprite);
            this.refreshDisplayObjects();
        }
        protected function onOwnerItemImageLoaded(_arg1:Event):void{
            this.m_ownerDisplayObj = (this.m_owner.getDisplayObject() as Sprite);
            this.refreshDisplayObjects();
        }
        private function refreshDisplayObjects():void{
            var _local1:int;
            for each (_local1 in DictionaryUtil.getKeys(this.m_displayContentMap)) {
                if (((this.m_displayContentMap[_local1]) && (this.m_ownerDisplayObj))){
                    if (this.m_ownerDisplayObj.contains(this.m_displayContentMap[_local1])){
                        this.m_ownerDisplayObj.removeChild(this.m_displayContentMap[_local1]);
                    };
                    this.m_ownerDisplayObj.addChild(this.m_displayContentMap[_local1]);
                    this.setDisplayPosition(this.m_displayContentMap[_local1], _local1);
                };
            };
        }
        protected function onItemImageLoaded(_arg1:Event):void{
            var _local2:Item = (_arg1.target as Item);
            if (!_local2){
                return;
            };
            _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
            this.refreshDisplayData();
        }
        protected function addToDisplay(_arg1:int, _arg2:String):Boolean{
            var _local4:Object;
            var _local7:DisplayObject;
            var _local3:Boolean;
            for each (_local4 in this.m_displayLocations) {
                if (_local4.id == _arg1){
                    _local3 = true;
                    break;
                };
            };
            if (!_local3){
                return (false);
            };
            var _local5:Item = Global.gameSettings.getItemByName(_arg2);
            if (!_local5){
                return (false);
            };
            var _local6:ItemImageInstance = _local5.getCachedImage(DEFAULT_IMAGE_NAME);
            if (!_local6){
                _local5.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 1);
            } else {
                _local7 = (_local6.image as DisplayObject);
                if (!this.m_ownerDisplayObj){
                    this.m_ownerDisplayObj = (this.m_owner.getDisplayObject() as Sprite);
                };
                if (this.m_ownerDisplayObj){
                    this.m_ownerDisplayObj.addChild(_local7);
                    this.setDisplayPosition(_local7, _arg1);
                } else {
                    this.m_isDirty = true;
                };
                this.m_displayContentMap[_arg1] = _local7;
            };
            return (true);
        }
        protected function refreshDisplayData():void{
            var _local2:int;
            var _local1:Array = this.m_owner.getDataForMechanic(this.m_dataSourceType);
            if (_local1){
                this.clearAddedDisplayObjects();
                _local2 = 0;
                while (_local2 < _local1.length) {
                    if (!_local1[_local2]){
                    } else {
                        this.addToDisplay(_local2, _local1[_local2]);
                    };
                    _local2++;
                };
            };
        }
        protected function clearAddedDisplayObjects():void{
            var _local1:int;
            var _local2:DisplayObject;
            for each (_local1 in DictionaryUtil.getKeys(this.m_displayContentMap)) {
                _local2 = this.m_displayContentMap[_local1];
                if (((((_local2) && (this.m_ownerDisplayObj))) && (this.m_ownerDisplayObj.contains(_local2)))){
                    this.m_ownerDisplayObj.removeChild(_local2);
                };
                delete this.m_displayContentMap[_local1];
            };
        }
        private function setDisplayPosition(_arg1:DisplayObject, _arg2:int):void{
            _arg1.x = this.m_displayLocations[_arg2].x;
            _arg1.y = (this.m_displayLocations[_arg2].y - _arg1.height);
        }
        public function isVisitEnabled():Boolean{
            return (true);
        }

    }
}//package Mechanics.ClientDisplayMechanics 
